//
//  UTIntervalNumbers.swift
//  iPhoneUTests
//
//  Created by Toj on 4/4/23.
//

import XCTest

class UTIntervalNumbers: XCTestCase {

    override func setUpWithError() throws {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }
    
    private var _triggerCount = 0
    var triggerCount: Int {
        get {
            return _triggerCount
        }
        set {
            _triggerCount = newValue
        }
    }
    
    private var _triggerTime: TimeInterval = 0
    var triggerTime: TimeInterval {
        get {
            
            return _triggerTime
        }
        set {
            _triggerTime = newValue
        }
    }
    
    func testExample() throws {
        print("-->UTests: start \n")
        
        let exeCount = Int(arc4random_uniform(200)) + 1
        print("-->UTests: exeCount: \(exeCount)")
        
        var intervalNumbers: Int = 0
        
        for _ in 0..<exeCount {
            
            let sleepTime = arc4random_uniform(5)
            print("-->UTests: sleepTime: \(sleepTime)")
            sleep(sleepTime)
            
            intervalNumbers = Int(arc4random_uniform(10)) + 1
            print("-->UTests: 次数间隔interval: \(intervalNumbers)")
            var isTrigger = testIntervalNumbers(interval: intervalNumbers)
            if !isTrigger {
                print("-->UTests: 次数限制time: \(triggerCount), ninterval: \(intervalNumbers)")
                continue
            }
            
            let timeInterval = TimeInterval(arc4random_uniform(30)) // + 1 * 60
            print("-->UTests: 时间间隔interval: \(timeInterval)")
            isTrigger = checkIntervalTimes(time: timeInterval)
            if !isTrigger {
                print("-->UTests: 时间限制time: \(triggerTime), interval: \(timeInterval)")
                continue
            }
            
            triggerCount = 1
            triggerTime = CFAbsoluteTimeGetCurrent()
            print("-->UTests: 加载广告count:\(triggerCount), time:\(triggerTime)  分隔符: --- ")
        }
        
        print("-->UTests: end \n")
    }

    func testPerformanceExample() throws {
        // This is an example of a performance test case.
        self.measure {
            // Put the code you want to measure the time of here.
        }
    }
}

extension UTIntervalNumbers {
    /*
     * 是否触发: 0: 不触发; 1: 触发
     * 触发改数: 1: 更后值; -: 不变
     * 大于间隔: 0: 不大于; 1: 大于
     当前间隔: 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 .......
     当前次数: 0 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 .......
     是否触发: 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 .......
     ------------------------------------------------------------
     当前间隔: 6 6 6 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 .......
     当前次数: 0 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 .......
     是否触发: 1 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 .......
     ------------------------------------------------------------
     当前间隔: 3 3 5 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 .......
     当前次数: 0 1 2 3 4 5 6 7 8 9 10 1 2 6 7 1 2 3 4 5 6 7 .......
     是否触发: 1 0 0 0 1 1 1 1 1 1 1 0 0 0 1 0 0 0 0 0 0 1 .......
     ------------------------------------------------------------
     */
    func testIntervalNumbers(interval: Int) -> Bool {
        guard interval >= 0 else { return false }
        // 第一次
        if triggerCount == 0 { return true }
        let isTrigger = triggerCount > interval
        //let currentCount = triggerCount
        triggerCount += 1
       // print("-->UTests: currentCount: \(currentCount), interval:\(interval),  triggerCount:\(triggerCount), isTrigger: \(isTrigger)")

        return isTrigger
    }
    
    func checkIntervalTimes(time interval: TimeInterval) -> Bool {
        guard interval > 0 else { return true }
        
        let currentTime = CFAbsoluteTimeGetCurrent()
        let astrictTime = triggerTime + interval
        let isTrigger = currentTime >= astrictTime
        
        // print("-->UTests: currentTime: \(currentTime), triggerTime:\(triggerTime),  interval:\(interval), isTrigger: \(isTrigger)")
        
        return isTrigger
    }
}
